#!/usr/bin/env bash

# Test for https://github.com/jdx/mise/discussions/5906
# Issue: Multiple version constraints with lockfile cause incorrect version selection
# and lockfile inconsistency

export MISE_LOCKFILE=1
export MISE_EXPERIMENTAL=1

# Clean start
rm -f mise.toml mise.lock
rm -rf "$MISE_DATA_DIR/installs/tiny"*

# Create a config with multiple version constraints
cat <<EOF >mise.toml
[tools]
tiny = ['latest', 'prefix:1.0', 'prefix:1.1']
EOF

# Create lockfile to enable lockfile mode
touch mise.lock

# Initial install should fetch all versions
echo "=== Initial install with multiple version constraints ==="
mise install
assert "mise ls tiny --json | jq -r 'length'" "3"

# Check that all versions are installed (prefix:1.0 installs 1.0.1, prefix:1.1 installs 1.1.0)
assert_contains "mise ls tiny" "1.0.1"
assert_contains "mise ls tiny" "1.1.0"
assert_contains "mise ls tiny" "3.1.0"

# Verify initial lockfile format uses array of tables
echo "=== Checking initial lockfile format ==="
cat mise.lock
assert_contains "cat mise.lock" '[[tools.tiny]]'
assert_contains "cat mise.lock" 'version = "1.0.1"'
assert_contains "cat mise.lock" 'version = "1.1.0"'
assert_contains "cat mise.lock" 'version = "3.1.0"'

# Check which version is considered "latest"
echo "=== Checking 'latest' version ==="
latest_version=$(mise ls tiny --json | jq -r '.[] | select(.requested_version == "latest") | .version')
assert "echo $latest_version" "3.1.0"

# Test that after removing and reinstalling a version,
# the lockfile maintains all versions
echo "=== Testing reinstall after removing a version ==="
rm -rf "$MISE_DATA_DIR/installs/tiny/1.1.0"
mise install

# Check that lockfile maintains format and all versions
echo "=== Checking lockfile after reinstall ==="
cat mise.lock
assert_contains "cat mise.lock" '[[tools.tiny]]'
assert_contains "cat mise.lock" 'version = "1.0.1"'
assert_contains "cat mise.lock" 'version = "1.1.0"'
assert_contains "cat mise.lock" 'version = "3.1.0"'

# Count how many versions are in the lockfile
lockfile_versions=$(grep -c "version =" mise.lock || echo 0)
echo "Number of versions in lockfile: $lockfile_versions"
assert "echo $lockfile_versions" "3"

# Show that all versions are still installed despite lockfile issues
echo "=== All versions still installed despite lockfile issues ==="
assert_contains "mise ls tiny" "1.0.1"
assert_contains "mise ls tiny" "1.1.0"
assert_contains "mise ls tiny" "3.1.0"

# Clean all versions and test install from broken lockfile
echo "=== Testing install from incomplete lockfile ==="
rm -rf "$MISE_DATA_DIR/installs/tiny"*
mise install

# Check what actually got installed
echo "=== Checking what was installed from incomplete lockfile ==="
mise ls tiny

# This should install all 3 versions from the lockfile
installed_count=$(mise ls tiny --json | jq -r 'length')
echo "Number of versions installed from lockfile: $installed_count"

# The test should fail if not all versions are installed
assert "echo $installed_count" "3"

# Clean up
rm -f mise.toml mise.lock
